package stat

import (
	"context"
	"github.com/hibiken/asynq"
	"github.com/zeromicro/go-zero/core/logx"
	"navi-mumbai/app/rpc/stat/pb"
	"navi-mumbai/app/timer/job/internal/svc"
	"navi-mumbai/common/key/dbkey"
	"time"
)

type UpdateUserLoginDailyStatHandler struct {
	svcCtx *svc.ServiceContext
}

func NewUpdateUserLoginDailyStatHandler(svcCtx *svc.ServiceContext) *UpdateUserLoginDailyStatHandler {
	return &UpdateUserLoginDailyStatHandler{
		svcCtx: svcCtx,
	}
}

func (l *UpdateUserLoginDailyStatHandler) ProcessTask(ctx context.Context, _ *asynq.Task) error {
	now := time.Now()
	loc, _ := time.LoadLocation(dbkey.DateTimeZone)
	st := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, loc).AddDate(0, 0, -1)
	et := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, loc).AddDate(0, 0, -1)
	in := pb.UserDailyLoginStatReq{
		StartTime: st.Format(dbkey.DateTimeCNFormat),
		EndTime:   et.Format(dbkey.DateTimeCNFormat),
	}
	_, err := l.svcCtx.StatSysRpc.UserDailyLoginStat(ctx, &in)
	if err != nil {
		logx.WithContext(ctx).Errorf("UserDailyLoginStat err: %v", err)
		return nil
	}
	return nil
}
